package com.wc.算法提高课.B第二章_搜索.Astar.第K短路;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

/**
 * @Author congge
 * @Date 2024/9/26 10:31
 * @description https://www.acwing.com/problem/content/180/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 1010, M = 20010, INF = 0x3f3f3f3f;
    static int[] h = new int[N], rh = new int[N], w = new int[M], ne = new int[M], e = new int[M];
    static int[] dist = new int[N];
    static boolean[] st = new boolean[N];
    static int n, m, S, T, K, idx = 1;
    static Queue<int[]> q = new LinkedList<>();

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        while (m-- > 0) {
            int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
            add(h, a, b, c);
            add(rh, b, a, c);
        }
        S = sc.nextInt();
        T = sc.nextInt();
        K = sc.nextInt();
        dijkstra();

        // 注意： 每条最短路中至少要包含一条边。所以没有最小的 0 作为第一个了
        if (S == T) K++;
        out.println(astar());

        out.flush();
    }

    static void add(int[] h, int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    static void dijkstra() {
        PriorityQueue<int[]> q = new PriorityQueue<>(Comparator.comparingInt(o -> o[0]));
        Arrays.fill(st, 0, n + 1, false);
        Arrays.fill(dist, 0, n + 1, INF);
        dist[T] = 0;
        q.add(new int[]{dist[T], T});
        while (!q.isEmpty()) {
            int[] t = q.poll();
            int ver = t[1];
            if (st[ver]) continue;
            st[ver] = true;
            for (int i = rh[ver]; i > 0; i = ne[i]) {
                int j = e[i];
                if (dist[j] > dist[ver] + w[i]) {
                    dist[j] = dist[ver] + w[i];
                    q.add(new int[]{dist[j], j});
                }
            }
        }
    }

    static int astar() {
        // int[]  0 估计值, 1 真实值, 2 点
        PriorityQueue<int[]> q = new PriorityQueue<>(Comparator.comparingInt(o -> o[0]));
        q.add(new int[]{dist[S], 0, S});
        int cnt = 0;
        while (!q.isEmpty()) {
            int[] t = q.poll();
            int ver = t[2], distance = t[1];
            if (ver == T) cnt++;
            if (cnt == K) return distance;
            for (int i = h[ver]; i > 0; i = ne[i]) {
                int j = e[i];
                // 如果是无效点直接跳过就行了
                if (dist[j] < INF)
                    q.add(new int[]{distance + w[i] + dist[j], distance + w[i], j});
            }
        }
        return -1;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
